package com.mazong.serverblog.task;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
//@EnableScheduling
public class TaskWork {

    private int workCount = 0;

    /**
     * fixedRate 的单位是毫秒
     */

    @Scheduled(fixedRate = 1000)
    public void taskCounter() {
        workCount ++;
        System.out.println("workCount=" + workCount);
    }

    @Scheduled(cron = "0/5 * * * * *")
    public void taskRun5() throws InterruptedException {
        Date dNow = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        System.out.println(Thread.currentThread().getName()+"=>使用cron："+ft.format(dNow));
    }
}
